package learnMethod

import (
	"fmt"
	"sort"
)

func MapTest01() {
	/*var a map[string]string*/
	/*	a := make(map[string]string)
		a["name"] = "张三"
		a["age"] = "李四"
		fmt.Println(a)*/
	studentMap := make(map[string]map[string]string)
	studentMap["student01"] = make(map[string]string, 3)
	studentMap["student02"] = make(map[string]string, 3)
	studentMap["student03"] = make(map[string]string, 3)

	studentMap["student01"]["name"] = "张三"
	studentMap["student01"]["age"] = "21"
	studentMap["student01"]["address"] = "北京"

	studentMap["student02"]["name"] = "李四"
	studentMap["student02"]["age"] = "22"
	studentMap["student02"]["address"] = "湖北"

	studentMap["student03"]["name"] = "王五"
	studentMap["student03"]["age"] = "23"
	studentMap["student03"]["address"] = "湖南"

	for k1, v1 := range studentMap {
		fmt.Println(k1)
		for k2, v2 := range v1 {
			fmt.Printf("k2=%v,v2=%v\n", k2, v2)
		}
		fmt.Println()
	}
	/*	fmt.Println(studentMap)
		fmt.Println(studentMap["student02"]["address"])
		studentMap["student02"]["address"] = "河南"
		fmt.Println(studentMap["student02"]["address"])
		delete(studentMap["student02"], "address")
		fmt.Println(studentMap["student02"]["address"])
		fmt.Println(studentMap)*/

}

//map切片
func SliceOfMap() {
	var monsters []map[string]string
	monsters = make([]map[string]string, 3)

	if monsters[0] == nil {
		monsters[0] = make(map[string]string, 2)
		monsters[0]["name"] = "李四"
		monsters[0]["age"] = "20"
	}

	if monsters[1] == nil {
		monsters[1] = make(map[string]string)
		monsters[1]["name"] = "王五"
		monsters[1]["age"] = "21"
	}
	if monsters[2] == nil {
		monsters[2] = make(map[string]string)
		monsters[2]["name"] = "赵六"
		monsters[2]["age"] = "22"
	}
	fmt.Println(monsters)

	for index, value := range monsters {
		fmt.Printf("下标：%v\n", index)
		for k, v := range value {
			fmt.Printf("k=%v,v=%v\n", k, v)
		}
		fmt.Println()
	}

	fmt.Println(len(monsters))
	fmt.Println(cap(monsters))

	newMonsters := map[string]string{
		"name": "威威啊",
		"age":  "33",
	}

	monsters = append(monsters, newMonsters)
	fmt.Println(monsters)
	fmt.Println(len(monsters))
	fmt.Println(cap(monsters))

	map1 := make(map[int]int, 10)
	map1[10] = 100
	map1[1] = 444
	map1[2] = 56
	map1[7] = 68

	fmt.Println(map1)
	var keys []int
	for k, _ := range map1 {
		keys = append(keys, k)
	}
	sort.Ints(keys)
	for _, k := range keys {
		fmt.Printf("map[%v]=%v\n", k, map1[k])
	}
	modify(map1)

	fmt.Println(map1)
}

func modify(mapone map[int]int) {
	mapone[10] = 900

}

type stu struct {
	name    string
	age     int
	address string
}

func LearnMapTest03() {
	student := make(map[string]stu, 10)
	stu1 := stu{"tom", 18, "北京"}
	stu2 := stu{"zhangsan", 18, "成都"}
	student["no1"] = stu1
	student["no2"] = stu2

	for k, v := range student {
		fmt.Printf("学生编号是%v,名字是%v,年龄是%v,住址是%v\n", k, v.name, v.age, v.address)
	}
}
func TestModifyUser() {
	users := make(map[string]map[string]string, 2)
	map1 := make(map[string]string, 2)
	map1["nickname"] = "zhangsan"
	map1["pwd"] = "123456"
	map2 := make(map[string]string, 2)
	map2["nickname"] = "lisi"
	map2["pwd"] = "456789"
	users["user01"] = map1
	users["user02"] = map2
	ModifyUser(users, "hhhh")
}
func ModifyUser(users map[string]map[string]string, name string) {
	if users[name] != nil {
		users[name]["pwd"] = "888888"
	} else {
		newMap := make(map[string]string)
		newMap["nickname"] = "陈威"
		newMap["pwd"] = "weiweiya"
		users["user03"] = newMap
	}

	for k1, v1 := range users {
		fmt.Println(k1)
		fmt.Println("v1[nickname]=", v1["nickname"])
		for k2, v2 := range v1 {
			fmt.Printf("k=%v,v=%v\n", k2, v2)
		}
	}
}
